package com.ocgs.code.service.impl;

import com.ocgs.base.core.constant.BaseConstants;
import com.ocgs.base.core.exception.BaseDAOException;
import com.ocgs.base.core.exception.BaseServiceException;
import com.ocgs.code.dao.impl.CodeDAOImpl;
import com.ocgs.code.model.Code;
import com.ocgs.code.model.CodeKey;
import com.ocgs.code.service.CodeService;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: bryant
 * Date: 10/21/12
 * Time: 3:40 PM
 * To change this template use File | Settings | File Templates.
 */
public class CodeServiceImpl implements CodeService {

    private CodeDAOImpl codeDAO;

    public void setCodeDAO(CodeDAOImpl codeDAO) throws BaseServiceException {
        this.codeDAO = codeDAO;
    }

    public int deleteByPrimaryKey(CodeKey codeKey) throws BaseServiceException {
       try{
           return codeDAO.deleteByPrimaryKey(codeKey);
       }catch (BaseDAOException e){
           throw new BaseServiceException(e);
       }

    }

    public int insert(Code record) throws BaseServiceException{
        try{
            return codeDAO.insert(record);
        }catch (BaseDAOException e){
            throw new BaseServiceException(e);
        }
    }
    public int insertSelective(Code record) throws BaseServiceException {
        try{
            return codeDAO.insert(record);
        }catch (BaseDAOException e){
            throw new BaseServiceException(e);
        }
    }

    
    public Code selectByPrimaryKey(CodeKey codeKey) throws BaseServiceException {
        try{
            return codeDAO.selectByPrimaryKey(codeKey);
        }catch (BaseDAOException e){
            throw new BaseServiceException(e);
        }
    }

    
    public int updateByPrimaryKeySelective(Code record) throws BaseServiceException {
        try{
            return codeDAO.updateByPrimaryKeySelective(record);
        }catch (BaseDAOException e){
            throw new BaseServiceException(e);
        }
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(Code record) throws BaseServiceException {
        return 0;  //To change body of implemented methods use File | Settings | File Templates.
    }


    public int updateByPrimaryKey(Code record) throws BaseServiceException {
        try{
            return codeDAO.updateByPrimaryKey(record);
        }catch (BaseDAOException e){
            throw new BaseServiceException(e);
        }
    }

    public List<Code> findCodeList(String codeTypeId) throws BaseServiceException {
        try{
            Code code = new Code();
            code.setCodeTypeId(codeTypeId);
            return codeDAO.findAllCode(code);
        }catch (BaseDAOException e){
            throw new BaseServiceException(e);
        }
    }

    public List<Code> findChildCodeList(String parentCodeTypeId,String parentCodeId) throws BaseServiceException {
        try{
            Code code = new Code();
            code.setParentCodeTypeId(parentCodeTypeId);
            code.setParentCodeId(parentCodeId);
            return codeDAO.findAllCode(code);
        }catch (BaseDAOException e){
            throw new BaseServiceException(e);
        }
    }

    public String retrieveAddress(String municipioId,String townId,String villageId) throws BaseServiceException{

        try {
            CodeKey key = new CodeKey();
            key.setCodeTypeId(BaseConstants.CodeType.CODE_TYPE_MUNICIPIO);
            key.setCodeId(municipioId);
            String municipio = codeDAO.selectByPrimaryKey(key).getCodeDesc();
            key.setCodeTypeId(BaseConstants.CodeType.CODE_TYPE_TOWN);
            key.setCodeId(townId);
            String town = codeDAO.selectByPrimaryKey(key).getCodeDesc();
            key.setCodeTypeId(BaseConstants.CodeType.CODE_TYPE_VILLAGE);
            key.setCodeId(villageId);
            String village = codeDAO.selectByPrimaryKey(key).getCodeDesc();
            StringBuilder sb = new StringBuilder();
            return sb.append(municipio).append(" ").append(town).append(" ").append(village).toString();
        } catch (BaseDAOException e) {
            throw new BaseServiceException(e);
        }
    }
}
